<?php
namespace app\admin\controller;

use \think\Controller;
use app\admin\controller\BaseController;
use app\admin\model\KeyWord as keywordModel;

class Keyword extends BaseController
{
    public function index(keywordModel $keyword)
    {
        $this->isLogin();
        $keywords = $keyword->where('is_use',0)->find();
        return view('hotWord/hotword',compact('keywords'));
    }
    public function update(keywordModel $keyword)
    {
        $this->isLogin();
        $keywords = input('post.');
        $keywords['keyword'] = str_replace('，',',',$keywords['keyword']);
        $keywords['keyword'] = str_replace(' ',',',$keywords['keyword']);
        $keywordArray = explode(',',$keywords['keyword']);
        $upData = [];
        foreach($keywordArray as $key => $value){
            if(!empty($value)){
                if(mb_strlen($value) > '5')  $this->error('关键词最大长度为5');
                $upData[] = $value;
            }
        }
        if(count($upData) > '12')  $this->error('关键词最多为12个');
        $data['id'] = $keywords['id'];
        $data['keyword'] = implode(',',$upData);
        $data['member_id'] = session('member_id');
        $data['member_name'] = session('member_name');
        $data['update_time'] = time();
        $result = $keyword->where('id',$data['id'])->update($data);
        if(!$result) $this->error('关键词修改失败');
        return $this->success('关键词修改成功');

    }

}